<?php
ob_start(); //funció php per evitar errors en el header
?>
<?php include ('encabezado.php') ?>
<form action="" method="POST">
<br><br>
<table align="center" border="2">
<tr>
<td align="center">
<h2>Delete Groups <br></h2>
Choose any group: <br>
<?php
$conexion = mysql_connect("$ip", "$userdb", "$passworddb");
	mysql_select_db("$db", $conexion);	
	
	$_SESSION['choosen'] = $choosengroup;	
	$q = "SELECT $db.Groups.Name, $db.Groups.GroupID  FROM Groups where name !='Teacher'";		
	$losgrupos= mysql_query($q, $conexion) or die ("problema con query");
	print "<select name=\"solucion\">\"<option value=\"todos\" name=\"todos\">All Groups</option>\""; //les lines de codi d'adalt hem serveixen per poder mostrar en el select el grups que es pode eliminar
	while ($registro = mysql_fetch_array($losgrupos))
	{
		$nombrecomleto = $registro['Name'];
		
		print "<option value=\"{$nombrecomleto}\" name=\"group\">{$nombrecomleto}</option>";
	} //bucle while que imprimeix una linea per cada registre al select
	print "</select><br>";
	echo "<input type='submit' name='delete' value='Delete group' >";
	
	echo "<div style='visibility: visible'>";
	echo "<form action='' method='POST'>";
		
	if($_SERVER['REQUEST_METHOD'] == 'POST')
			{
				$conexion = mysql_connect("$ip", "$userdb", "$passworddb");
				mysql_select_db("$db", $conexion);	
				$_SESSION['choosen'] = $choosengroup;				
				$name = $_POST['solucion'];				
				$q = "SELECT $db.Groups.GroupID FROM Groups where name='$name'";		
				$gid= mysql_query($q, $conexion) or die ("problema con query");
				$groupid = mysql_fetch_array($gid);
				$groupid = $groupid['GroupID'];
				$q2 = "SELECT $db.Users.Name, $db.Users.Surname FROM Users where GroupID='$groupid'";
				$users = mysql_query($q2, $conexion) or die ("problema con query");
					$contador=1;
				echo "<br><table border='4'><font color='Black'><td colspan='4' align='center' bgcolor='666666'><font color='#FFFFFF'><strong>Student List</strong></font></td><tr>";
					while ($nombres = mysql_fetch_array($users))
					{
						
						$nombrecompleto = $nombres['Surname'].', '.$nombres['Name'];						
						echo "<td><font color='Black'>".$contador." ".$nombrecompleto."</font></td></tr>";
						$contador++;
					
					}
				echo "</table>";
				$_SESSION['grupo'] = $name;
				echo "<div id='confirmar'>";
				echo "<br>";
				echo "<font size=+2 color='Black'><b><font size=+1>Do you agree? All the students and all their Marks will be deleted</font></b></font>"; 
				echo "<form method='POST' action=''>";
				echo "<br>";
				echo ' Yes<input type="checkbox" name="yes" >';
				echo " No<input type='checkbox' name='no'>";
				echo "<input type='submit' name=borrar value='Confirm'>";
				echo "<input type='hidden' value='true' name='enviado'>";
				echo "</div>";
			}
	echo "</form>";
	echo "</div>";
	 //aquets echo imprimeixen un div en principi ocult, es la part de confirmació que es demanava, quan l'usuari intenta esborrar el grup surt el missatge de confirmació amb el corresponent botó per esborrar o no esborrar 
?>
<?php
	if(isset($_POST['solucion']))
	{
		$_SESSION['choosen'] = '';
		$group = $name;
		if(!isset($_POST['yes']) && isset($_POST['no']))
		{
			
				echo '<br><font color="green"><h3>Operation has been canceled</h3></font>';
				header("refresh:2;url=deletegroups.php");//refresh de la pàgina als 2 segons
				//dins aquest if es comprova que l'usuari a triat no
			
		}
		elseif(isset($_POST['yes']))
		{
			//aquí s'exucta el següent codi ja que l'usuari a triat si
			if ( $_POST['yes'] == 'on') 
			{
				
				$query2 = "SELECT GroupID FROM Groups where Name='$group'";
				$groupid = mysql_query($query2, $conexion) or die("There was a connection problem"); 
				$row = mysql_fetch_array($groupid);
				echo $row;
				$groupid = $row['GroupID'];//guardam el grupid
				$query = "SELECT UserID FROM Users where GroupID='$groupid'"; 
				$ids = mysql_query($query, $conexion) or die("There was a connection problem");//guardam L'UserID
				while($registro = mysql_fetch_array($ids))
				{	$userid = $registro['UserID'];
					$q = "DELETE FROM Marks where UserID = '$userid'"; //dins aquest bucle borram totes les notes amb l'id guardada del usuaris
					
					$del = mysql_query($q, $conexion) or die ("There was a connection problem");
					
				}
				$query21 = "DELETE FROM Users where GroupID='$groupid'"; //borram els usuaris del grup
				
				$delete = mysql_query($query21, $conexion) or die ("There was a connection problem");				
				$query3 = "DELETE FROM Groups where Name='$group'";
				
				$exec = mysql_query($query3, $conexion) or die ("There was a connection problem"); //y ara borram el grup
				if(mysql_query($q) && mysql_query($query21) && mysql_query($query3))
				{
					echo "<br><h3>The group has been deleted</h3>";
					//si s'han executat bé les comandes anteriors, a les hores mostrarà el missatge de grup eliminat
				}
				else
				{
					echo "<br><h3>An error ocurred</h3>";
					//si no aquest altre 
				}
				
			header("refresh:2;url=deletegroups.php");//refresh de la pàgina als 2 segons
				
			}	
		} 
			
	}
?>
</form>
</table>
<?php include ('pie.php') ?>
<?php
ob_end_flush(); //funció php per evitar errors en el header
?>